package com.hc.controller;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.domain.Permission;
import com.hc.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * 权限controller
 *
 * @author 六月Bing
 */
@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @PostMapping
    public Result create(@RequestBody Permission permission) {
        boolean res = permissionService.save(permission);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result updateById(@RequestBody Permission permission) {
        boolean res = permissionService.updateById(permission);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result disableById(@PathVariable Serializable[] id) {
        boolean res = false;
        for (Serializable serializable : id) {
            Permission permission = permissionService.getById(serializable);
            if (permission != null) {
                //修改状态为不可用
                if (permission.getStatus() == 1) {
                    permission.setStatus(2);
                    res = permissionService.updateById(permission);
                } else {
                    return ResultUtil.fail("禁用失败");
                }
            } else {
                return ResultUtil.fail("禁用失败");
            }
        }
        return res ? ResultUtil.success("禁用成功") : ResultUtil.fail("禁用失败");
    }

    @GetMapping("{id}")
    public Result<Permission> getById(@PathVariable Serializable id) {
        Permission res = permissionService.getById(id);
        return ResultUtil.<Permission>success("查询成功").setData(res);
    }

//    @GetMapping
//    public Result<List<Permission>> getByCondition(UserQuery condition) {
//        List<Permission> list = permissionService.queryByCondition(condition);
//        return ResultUtil.success(200, "查询成功", list);
//    }
}